package actions;

//~--- non-JDK imports --------------------------------------------------------
import beans.ProfileBean;
import beans.RegisterationFormBean;
import client.UserModel;
import dto.JfMembers;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class ProfileAction extends org.apache.struts.action.Action {

    public ProfileAction() {
        System.out.println("I am inside edit profile action ");
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward = new ActionForward();
        HttpSession session = request.getSession(false);      
        JfMembers mem = (JfMembers) session.getAttribute("userBean");
        
         
        String username = mem.getUsername();
       
        String fname = ((ProfileBean) form).getfName();
        mem.setFName(fname);
        String lname = ((ProfileBean) form).getlName();
        mem.setLName(lname);
        String mail = ((ProfileBean) form).getMail();
        mem.setEMail(mail);
        String radio = ((ProfileBean) form).getGender();
        if(radio== null) 
        {radio=mem.getGender();
        System.out.println(radio);}
        else {mem.setGender(radio);
        System.out.println(radio);}
        String country = ((ProfileBean) form).getCountry();       
        if(country.equals("0"))
        {         
            System.out.println("empty"+mem.getCountry());
        }
        else{
         mem.setCountry(country);
            System.out.println("non empty "+country);
        }
          
         File file=new File(getServlet().getServletContext().getRealPath("/")+"upload");
          boolean exist=file.exists();
          if(exist)
          {
              System.out.println("Exists");
          }
          else
          {
              System.out.println("Not Exist");
              file.mkdir();
              System.out.println("file Created");
          
          }
        
        FormFile myFile=((ProfileBean)form).getTheFile();    
        String contentType = myFile.getContentType();
        String fileName    = myFile.getFileName();
        if(fileName.equals(""))
        {
            System.out.println("empty"); 
            System.out.println(mem.getAvatar());
        }
        else
        {
        byte[] fileData    = myFile.getFileData();
        String filePath=getServlet().getServletContext().getRealPath("/")+"upload"+"/"+username;
        File f = new File(filePath);
        f.mkdir();
        String imgPath=filePath+"\\"+fileName;
       if(!fileName.equals(""))
        {   System.out.println("Server path:" +filePath);
            File fileToCreate = new File(filePath, fileName);

            if(!fileToCreate.exists())
            {
              FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
              fileOutStream.write(myFile.getFileData());
              fileOutStream.flush();
              fileOutStream.close();
              System.out.println("File Uploaded");
            }
        }
         mem.setAvatar("upload"+"/"+username+"/"+fileName);
            System.out.println(mem.getAvatar());
        }
               
           UserModel model = new UserModel();
        boolean editprofile = model.editprofile(mem);
        if(editprofile)
        {
            forward = mapping.findForward("done");
            session.setAttribute("userBean", mem);
        }
       else
        {
            forward = mapping.findForward("fail");
        }  
        return forward;

    }
    // =========================================1=====================================
//    public ActionForward editpw(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                HttpServletResponse response)
//            throws Exception {
//        ActionForward forward = new ActionForward();
//
//        System.out.println("Holaaaaaaaaaaa");
//
//        HttpSession session = request.getSession(false);
//        JfMembers   mem     = (JfMembers) session.getAttribute("userBean");
//        String      un      = mem.getUsername();
//        String      pw      = ((ProfileBean) form).getPassword();
//
//        System.out.println(un + pw);
//
//        UserModel model = new UserModel();
//        boolean   res   = model.editpw(pw, un);
//       mem.setPassword(pw);
//        if (res) {
//            forward = mapping.findForward("done");
//            session.setAttribute("userBean", mem);
//        } else {
//            forward = mapping.findForward("fail");
//        }
//
//        return forward;
//    }
//
//    // ===========================================2======================================
//    public ActionForward editfname(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                   HttpServletResponse response)
//            throws Exception {
//        ActionForward forward = new ActionForward();
//        HttpSession   session = request.getSession(false);
//        JfMembers     mem     = (JfMembers) session.getAttribute("userBean");
//        String        un      = mem.getUsername();
//        String        fn      = ((ProfileBean) form).getfName();
//
//        System.out.println(un + fn);
//
//        UserModel model = new UserModel();
//        boolean   res   = model.editfname(fn, un);
//        mem.setFName(fn);
//        if (res) {
//            forward = mapping.findForward("done");
//            session.setAttribute("userBean", mem);
//        } else {
//            forward = mapping.findForward("fail");
//        }
//
//        return forward;
//    }
//
//    // ==========================================3==========================================
//    public ActionForward editlname(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                   HttpServletResponse response)
//            throws Exception {
//        ActionForward forward = new ActionForward();
//        HttpSession   session = request.getSession(false);
//        JfMembers     mem     = (JfMembers) session.getAttribute("userBean");
//        String        un      = mem.getUsername();
//        String        ln      = ((ProfileBean) form).getlName();
//
//        System.out.println(un + ln);
//
//        UserModel model = new UserModel();
//        boolean   res   = model.editlname(ln, un);
//         mem.setLName(ln);
//        if (res) {
//            forward = mapping.findForward("done");
//            session.setAttribute("userBean", mem);
//        } else {
//            forward = mapping.findForward("fail");
//        }
//
//        return forward;
//    }
//
//    // =================================================4================================
//    public ActionForward editgender(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                    HttpServletResponse response)
//            throws Exception {
//        ActionForward forward = new ActionForward();
//        HttpSession   session = request.getSession(false);
//        JfMembers     mem     = (JfMembers) session.getAttribute("userBean");
//        String        un      = mem.getUsername();
//        String        gen     = ((ProfileBean) form).getGender();
//
//        System.out.println(un + gen);
//
//        UserModel model = new UserModel();
//        boolean   res   = model.editgender(gen, un);
//        mem.setGender(gen);
//        if (res) {
//            forward = mapping.findForward("done");      
//            session.setAttribute("userBean", mem);
//        System.out.println("session id is"+ session.getId());
//        } else {
//            forward = mapping.findForward("fail");
//        }
//       
//        return forward;
//    }
//
//    // ============================================5======================================
//    public ActionForward editcountry(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                     HttpServletResponse response)
//            throws Exception {
//        ActionForward forward = new ActionForward();
//        HttpSession   session = request.getSession(false);
//        JfMembers     mem     = (JfMembers) session.getAttribute("userBean");
//        String        un      = mem.getUsername();
//        String        coun    = ((ProfileBean) form).getCountry();
//
//        System.out.println(un + coun);
//
//        UserModel model = new UserModel();
//        boolean   res   = model.editcountry(coun, un);
//         mem.setCountry(coun);
//        if (res) {
//            forward = mapping.findForward("done");
//            session.setAttribute("userBean", mem);
//        } else {
//            forward = mapping.findForward("fail");
//        }
//
//        return forward;
//    }
//
//    // ========================================6============================================
//    public ActionForward editemail(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                   HttpServletResponse response)
//            throws Exception {
//        ActionForward forward = new ActionForward();
//        PrintWriter   out     = response.getWriter();
//        HttpSession   session = request.getSession(false);
//        JfMembers     mem     = (JfMembers) session.getAttribute("userBean");
//        String        un      = mem.getUsername();
//        String        mail    = ((ProfileBean) form).getMail();
//
//        System.out.println(un + mail);
//
//        UserModel model = new UserModel();
//        boolean   res   = model.editmail(mail, un);
//           mem.setEMail(mail);
//        if (res) {
//            forward = mapping.findForward("done");
//            session.setAttribute("userBean", mem);
//            out.print("Mail Updated");
//        } else {
//            forward = mapping.findForward("fail");
//        }
//
//        return forward;
//    }
//
//    // =======================================================================================
//    public ActionForward editavatar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//                                    HttpServletResponse response)
//            throws Exception {
//        System.out.println("edit avatar");
//
//        ActionForward forward = new ActionForward();
//        HttpSession   session = request.getSession(false);
//        JfMembers     mem     = (JfMembers) session.getAttribute("userBean");
//        String        un      = mem.getUsername();
//
//        // ==========================================
//        FormFile myFile      = ((ProfileBean) form).getTheFile();
//        String   contentType = myFile.getContentType();
//        String   fileName    = myFile.getFileName();
//        byte[]   fileData    = myFile.getFileData();
//        String   filePath    = getServlet().getServletContext().getRealPath("/") + "upload" + "/" + un;
//        File     f           = new File(filePath);
//
//        f.mkdir();
//
//        String imgPath = filePath + "\\" + fileName;
//
//        if (!fileName.equals("")) {
//            System.out.println("Server path:" + filePath);
//
//            File fileToCreate = new File(filePath, fileName);
//
//            if (!fileToCreate.exists()) {
//                FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
//
//                fileOutStream.write(myFile.getFileData());
//                fileOutStream.flush();
//                fileOutStream.close();
//                System.out.println("File Uploaded");
//            }
//        }
//
//        // ==========================================
//        UserModel model = new UserModel();
//        boolean   res   = model.editavatar(imgPath, un);
//          mem.setAvatar("upload"+"/"+un+"/"+fileName);  ////////////////////////////////////////////
//        if (res) {
//            forward = mapping.findForward("done");
//            session.setAttribute("userBean", mem);
//        } else {
//            forward = mapping.findForward("fail");
//        }
//
//        return forward;
//    }
}


//~ Formatted by Jindent --- http://www.jindent.com
